import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { UserInfoType } from "@/types/user";

export const useUserStore = defineStore('user',
    () => {
        // userToken 为字符串或空
        const userToken = ref<string>()

        const userInfo = ref<UserInfoType>()

        const setUserToken = (token: string) => {
            userToken.value = token
        }

        const setUserInfo = (info: UserInfoType) => {
            userInfo.value = info
        }

        const clearUserInfo = () => {
            userInfo.value = undefined
            userToken.value = undefined
        }



        return {
            userToken,
            userInfo,
            setUserToken,
            setUserInfo,
            clearUserInfo,
        }
    },
    {
        persist: {
            enabled: true,
            strategies: [
                {
                    key: 'user',
                    storage: localStorage,
                },
            ]
        },
    }
)